const BaseController = require('./baseController')

class UserController extends BaseController {
  get modelName () {
    return 'User'
  }
  // 注册
  async signup () {
    const { ctx } = this
    const rule = {
      name: { type: 'string', required: true, message: '必填项' },
      password: { type: 'string', required: true, message: '必填项' },
      email: { type: 'email', required: true, message: '必填项' }
    }

    const signupMsg = ctx.request.body
    await ctx.validate(rule, signupMsg)
    signupMsg.password = ctx.helper.encrypt(signupMsg.password)
    const result = await ctx.service.user.signup(signupMsg)
    ctx.body = result
  }

  // 登录
  async signin () {
    const { ctx } = this
    const rule = {
      name: { type: 'string', required: true, message: '必填项' },
      password: { type: 'string', required: true, message: '必填项' }
    }
    const signinMsg = ctx.request.body
    await ctx.validate(rule, signinMsg)
    signinMsg.password = ctx.helper.encrypt(signinMsg.password)
    const result = await ctx.service.user.signin(signinMsg)
    ctx.body = result
  }

  async signinWxchat () {
    const { ctx } = this
    const rule = {
      code: { type: 'string', required: true, message: '必要参数' }
    }
    const signinMsg = ctx.request.body
    await ctx.validate(rule, signinMsg)
    const result = await ctx.service.user.signinWxchat(signinMsg)
    // console.log('signin.wxchat result=========', result)
    ctx.body = result
  }

  // 登出
  async signout () {
    const { ctx } = this
    const res = {}
    res.code = 1
    res.msg = '登出系统成功'
    ctx.body = res
  }

  // 查询用户信息
  async findUser () {
    const { ctx } = this
    const userMsg = {
      _id: ctx.params._id
    }
    const result = await ctx.service.user.findUser(userMsg)
    ctx.response.body = result
  }

  // 更新用户信息
  async updateUser () {
    const { ctx } = this
    const rule = {
      _id: { type: 'string', required: true, message: '必填项' },
      email: { type: 'string', required: true, message: '必填项' }
    }
    const userMsg = ctx.request.body
    await ctx.validate(rule, userMsg)
    const result = await ctx.service.user.updateUser(userMsg)
    ctx.body = result
  }

  // 用户注销
  async logOff () {
    const { ctx } = this
    const rule = {
      _id: { type: 'string', required: true, message: '必填项' }
    }
    const userMsg = ctx.request.body
    await ctx.validate(rule, userMsg)
    const result = await ctx.service.user.logOff(userMsg)
    ctx.body = result
  }

  // 超级管理员删除用户
  async deleteUser () {
    const { ctx } = this
    const rule = {
      _id: { type: 'string', required: true, message: '必填项' },
      userRole: { type: 'enum', values: ['3'] },
      deleteUserId: { type: 'string', required: true, message: '必填项' }
    }
    const userMsg = ctx.request.body
    await ctx.validate(rule, userMsg)
    const deleteUserMsg = {
      _id: userMsg.deleteUserId
    }
    const result = await ctx.service.user.deleteUser(deleteUserMsg)
    ctx.body = result
  }
}

module.exports = UserController
